﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Sylvain.Manager;
using System.IO;
using Sylvain.Model;

public partial class Admin_EditPastPublications : System.Web.UI.Page
{
    ISylvainManager objManger = new SylvainManager();
    static int PageId;
    /// <summary>
    /// Display content of the Page.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        if (!Page.User.Identity.IsAuthenticated)
            Response.Redirect("~/Admin/AdminLogin.aspx", false);

        if (!IsPostBack)
        {


            PageId = objManger.GetPageId("past_ publications");
            //FckEditor1.Value= objManger.GetPageContent(PageId);
            //Bind Publications Details with Gridview
            BindPublicationsGrid();
        }
    }

    /// <summary>
    /// Bind NewsSection Details with Gridview
    /// </summary>
    protected void BindPublicationsGrid()
    {
        grvPublications.DataSource = objManger.GetAllPublications();
        grvPublications.DataBind();

    }

    /// <summary>
    /// Insert/Update content of the Page.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ISylvainManager objManger = new SylvainManager();
        //Get current user
        MembershipUser userInfo = Membership.GetUser();
        if (userInfo != null)
        {
            if (hidId.Value != "")
            {
                Publication objUpdatePub = new Publication();
                objUpdatePub.PublicationsId = int.Parse(hidId.Value);
                objUpdatePub.PageDescription = this.FckEditor1.Value;
                string filename = "";
                if (fileuploadPdf.HasFile)
                {
                    filename = Guid.NewGuid() + Path.GetExtension(fileuploadPdf.FileName);
                    string imagePath = Server.MapPath("~/Publications/");
                    fileuploadPdf.SaveAs(imagePath + filename);
                }

                objUpdatePub.PubFileName = filename;
                objUpdatePub.UpdatedBy = userInfo.UserName;
                objManger.EditPublication(objUpdatePub);

                BindPublicationsGrid();
            }
            else
            {
                objManger.InsertPageContent(PageId, FckEditor1.Value, userInfo.UserName);
            }
        }
        else
        {
            Response.Redirect("~/Admin/AdminLogin.aspx", false);
        }

        lblError.Text = "Content has been updated sucessfully.";
    }

    /// <summary>
    /// Delete/Update Record in Gridview
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvPublications_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);

                //Delete News
                objManger.DeletePublication(ID);
                BindPublicationsGrid();

            }
            if (e.CommandName == "Update")
            {
                hidId.Value = e.CommandArgument.ToString();
                lblError.Text = "";
                trUploadPDF.Visible = true;
                this.hlnkDownload.Visible = false;
            }
        }
        catch
        {
            lblError.Text = "Operation could not be performed.";
        }
    }
    /// <summary>
    /// Updating a Record
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvPublications_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            GridViewRow row = (GridViewRow)grvPublications.Rows[e.RowIndex];
            FckEditor1.Value = ((Label)row.FindControl("lblDescription")).Text;
            string filename = ((HiddenField)row.FindControl("hidFileName")).Value;

            if (File.Exists(Server.MapPath("~/Publications/"+filename)))
            {
                this.hlnkDownload.Visible = true;
                this.hlnkDownload.NavigateUrl = "~/Publications/" + filename;
            }
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }
    }
    /// <summary>
    /// Display Page Index
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvPublications_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvPublications.PageIndex = e.NewPageIndex;
        BindPublicationsGrid();
    }

    protected void grvPublications_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Publication row = (Publication)e.Row.DataItem;
        if (row != null)
        {
            HyperLink lnk = (HyperLink)e.Row.FindControl("hlnkPubFileName");
            HiddenField hid = (HiddenField)e.Row.FindControl("hidFileName");
            if (lnk != null && hid != null)
            {
                if (hid.Value != "")
                {
                    if (File.Exists(Server.MapPath("~/Publications/" + hid.Value)))
                    {
                        lnk.Visible = true;
                        lnk.NavigateUrl = "~/Publications/" + hid.Value;
                    }
                }

            }
        }
    }
    protected void grvPublications_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
}